package com.intellagric.service.traceability.Impl.trace_produce_warehouse;

import com.github.pagehelper.PageHelper;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.traceability.ProduceWarehouseDao;
import com.intellagric.mapper.TraceProduceWarehouseMapper;
import com.intellagric.module.traceability.ProduceWarehouseVo;
import com.intellagric.pojo.TraceProduceWarehouse;
import com.intellagric.pojo.TraceProduceWarehouseExample;
import com.intellagric.service.traceability.trace_produce_warehouse.ProduceWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/11/3
 * @DESC： 农产品库存信息服务实现
 */
@Service
public class ProduceWarehouseServiceImpl implements ProduceWarehouseService {

    @Autowired
    private TraceProduceWarehouseMapper produceWarehouseMapper;

    @Autowired
    private ProduceWarehouseDao produceWarehouseDao;

    /**
     * 添加农产品库存信息
     * @param produceWarehouse
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addProduceWarehouse(TraceProduceWarehouse produceWarehouse) {
        if (produceWarehouseMapper.insertSelective(produceWarehouse) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除农产品库存信息
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteProduceWarehouse(String id) {
        if (produceWarehouseMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改农产品库存信息
     * @param produceWarehouse
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editProduceWarehouse(TraceProduceWarehouse produceWarehouse) {
        if (produceWarehouseMapper.updateByPrimaryKeySelective(produceWarehouse) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询农产品库存信息
     * @param id
     * @return TraceProduceWarehouse
     */
    @Override
    public TraceProduceWarehouse getProduceWarehouseById(String id) {
        return produceWarehouseMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询农产品库存信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getProduceWarehouseList() {
        TraceProduceWarehouseExample produceWarehouseExample = new TraceProduceWarehouseExample();
        produceWarehouseExample.createCriteria().andIdIsNotNull();
        List<TraceProduceWarehouse> produceWarehouseList = produceWarehouseMapper.selectByExample(produceWarehouseExample);
        return LayuiDataGridResult.success().add(produceWarehouseList,produceWarehouseList.size());
    }

    /**
     * 分页查询农产品库存信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getProduceWarehousePage(int page, int rows) {
        PageHelper.startPage(page,rows);
        TraceProduceWarehouseExample produceWarehouseExample = new TraceProduceWarehouseExample();
        produceWarehouseExample.createCriteria().andIdIsNotNull();
        List<TraceProduceWarehouse> produceWarehouseList = produceWarehouseMapper.selectByExample(produceWarehouseExample);
        return LayuiDataGridResult.success().add(produceWarehouseList,produceWarehouseList.size());
    }
    /**
     * 分页查询农产品库存信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getProduceWarehousePage(int page, int rows,String createBy) {
        PageHelper.startPage(page,rows);
        TraceProduceWarehouseExample produceWarehouseExample = new TraceProduceWarehouseExample();
        produceWarehouseExample.createCriteria().andCreateByEqualTo(createBy);
        List<TraceProduceWarehouse> produceWarehouseList = produceWarehouseMapper.selectByExample(produceWarehouseExample);
        return LayuiDataGridResult.success().add(produceWarehouseList,produceWarehouseList.size());
    }

    /**
     * 多条件查询农产品库存信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getProduceWarehouseByVo(ProduceWarehouseVo produceWarehouseVo) {
        List<TraceProduceWarehouse> produceWarehouseList = produceWarehouseDao.selectProduceWarehouseByVo(produceWarehouseVo);
        return LayuiDataGridResult.success().add(produceWarehouseList,produceWarehouseList.size());
    }
}
